﻿$(function () {
    $("#FileUpload").bind("change", function () {
        //开始提交
        $("#form1").ajaxSubmit({
            beforeSubmit: function (formData, jqForm, options) {
                //隐藏上传按钮
                $(".files").hide();
                //显示LOADING图片
                $("#idProcess").show();
            },
            success: showResponse,
            error: function (data, status, e) {
                alert("上传失败，错误信息：" + e);
                $(".files").show();
                $("#idProcess").hide();
            },
            url: "../../Tools/upload_ajax.ashx?action=MultipleFile",
            type: "post",
            dataType: "json",
            timeout: 60000
        });
    });

    //第一张图加边框
    $("#show_list ul li img:first").addClass("current");
});

//上传图片成功的处理类
function showResponse(data, textStatus) {
    if (data.msg == 1) {
        var str = "<li><img onclick=\"focus_img(this);\" src=\"" + data.msbox + "\" bigsrc=\"" + data.msbox + "\"><br /><a href=\"" + data.msbox + "\" target=\"_blank\">原图</a><a href=\"javascript:void(0)\" onclick=\"dlstItems_Command(this,'" + data.msgid + "','" + data.msbox + "');\">删除</a><input name=\"hideFiles\" type=\"hidden\" value=\"" + data.msbox + "\"></li>";

        $("#show_list ul").append(str);
    } else {
        alert(data.msbox);
    }
    $(".files").show();
    $("#idProcess").hide();
}

//删除图片及删除数据库处理方法
function dlstItems_Command(obj, pid, imgurl) {
    var node = $(obj).parent(); //要删除的LI节点
    //var iNum = $(".imgItems ul li").index(node); //所在集合的索引

    //开始提交删除
    $.ajax({
        type: "post",
        url: "../../Tools/upload_ajax.ashx?action=DeleteFile",
        data: {
            pid: function () {
                return pid;
            },
            delfile: function () {
                return imgurl;
            }
        },
        dataType: "json",
        success: function (data, textStatus) {
            if (data.msg == 0) {
                alert(data.msbox);
            }
        },
        error: function (data, status, e) {
            alert("删除文件失败:" + e);
        }
    });

    //删除该无素父节点
    node.remove();
}

//当前选择图片
function focus_img(obj) {
    $("#show_list ul li img").removeClass("current");
    $(obj).addClass("current");
}